package com.dylan.历史.java8.新日期;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

/**
 * Created by Dylan on 2018/7/28.
 */
public class 日期校正器 {

    public static void main(String[] args) {
        // 返回指定日期及以后第一次出现星期二的日期,有可能返回指定日期
        LocalDate firstTuesday1 = LocalDate.of(2018, 8, 1).with(
                TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY)
        );
        System.out.println(firstTuesday1);

        // 返回指定日期的下一个星期中的星期二
        LocalDate firstTuesday2 = LocalDate.of(2018, 8, 1).with(
                TemporalAdjusters.next(DayOfWeek.SATURDAY)
        );
        System.out.println(firstTuesday2);

        // 返回当月的第3个星期一
        LocalDate localDate1 = LocalDate.of(2018, 7, 31).with(
                TemporalAdjusters.dayOfWeekInMonth(3, DayOfWeek.MONDAY)
        );
        System.out.println(localDate1);

        // 返回当月的最后一个星期一
        LocalDate localDate2 = LocalDate.of(2018, 7, 31).with(
                TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY)
        );
        System.out.println(localDate2);

        // 返回当月的最后一天
        LocalDate localDate3 = LocalDate.of(2018, 7, 13).with(
                TemporalAdjusters.lastDayOfMonth()
        );
        System.out.println(localDate3);

        // 返回指定日期的下一个工作日(周一到周五)
        TemporalAdjuster NEXT_WORKDAY = TemporalAdjusters.ofDateAdjuster(w -> {
            LocalDate result = w;
            do {
                result = result.plusDays(1);
            } while (result.getDayOfWeek().getValue() >= 6);
            return result;
        });
        LocalDate localDate4 = LocalDate.of(2018,7,27).with(NEXT_WORKDAY);
        System.out.println(localDate4);
    }
}
